<?php
/**
 * Author: LaiGuiming
 * Time: 2017-08-14 14:58
 * Copyright (C) 2016 Longyan Wehome Message CO.,LTD.
 */

namespace app\admin\logic;

/**
 * 组件封装类
 * Class UnitCreate
 * @package app\admin\logic
 */
class UnitCreate
{
    /**
     * 复选框组件
     * @param string $label
     * @param string $field
     * @param $data
     * @param string $value
     * @param string $name
     * @param string $checked
     * @param bool $required
     */
    public function checkbox($label='复选框',$field='field',$data,$value='value',$name='name',$checked='',$required=false){
        if(!is_array($checked)){
            $checked = explode(',',$checked);
        }

        $str='<div class="checkbox-inline i-checks">';
        foreach ($data as $vo){
            $val=$vo[$value];
            $text=$vo[$name];
            $check_flag='';
            if(in_array($val,$checked)){
                $check_flag='checked';
            }

            $required_str='';
            if($required){
                $required_str='datatype="*" nullmsg="请选择'.$label.'！"';
            }

            $str.='<span><input type="checkbox" value="'.$val.'" name="'.$field.'" '.$check_flag.'  '.$required_str.'> '.$text.'
                                    </span>';
        }
        $str.='</div>';
        echo $str;
    }

    /**
     * 百度编辑器组件
     * @param string $name
     * @param string $style
     * @param string $value
     * @param string $datatype
     * @param string $nullmsg
     * @param string $dom
     */
    public function ueditor($name='',$style='',$value='',$datatype='',$nullmsg='',$dom='editor'){
        $validate='';
        if($datatype!=''){
            $validate.='datatype="'.$datatype.'" ';
        }

        if($nullmsg!=''){
            $validate.='nullmsg="'.$nullmsg.'" ';
        }

        $str='<script style="'.$style.'" id="'.$dom.'" name="'.$name.'" type="text/plain" '.$validate.'>'.$value.'</script>';

        echo $str;
    }

    /**
     * 图片上传组件
     * @param string $name
     * @param string $value
     * @param string $datatype
     * @param string $nullmsg
     * @param string $dom
     */
    public function fileInput($name='',$value='',$datatype='',$nullmsg='',$dom='fileInput'){
        $validate='';
        if($datatype!=''){
            $validate.='datatype="'.$datatype.'" ';
        }

        if($nullmsg!=''){
            $validate.='nullmsg="'.$nullmsg.'" ';
        }

        $str='<input id="'.$dom.'" type="file" >
                                <input id="'.$dom.'_previewImg"  type="hidden" name="'.$name.'" value="'.$value.'"  '.$validate.' >';

        echo $str;
    }

    /**
     * 模态窗口
     * @param string $node
     * @param string $title
     */
    public function modal($node='',$title='',$html=''){
        $str='<div class="modal inmodal fade" id="'.$node.'" tabindex="-1" role="dialog"  aria-hidden="true">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                            <h4 class="modal-title" id="videoName">'.$title.'</h4>
                        </div>
                        <div class="modal-body" id="'.$node.'_body">
                            '.$html.'
                        </div>
                    </div>
                </div>
            </div>
            ';
        echo $str;
    }

    /**
     * 创建工具栏
     * @param string $tool  add,del,search
     */
    public function toolbar($tool=''){
        $str="";
        if($tool==""){
            $str='<button title="普通查询" class="btn btn-default btn-sm table-search" type="button"><i class="glyphicon glyphicon-search"></i></button>
                    <button class="btn btn-primary btn-sm table-add" type="button"><i class="fa fa-plus"></i> 添加</button>
                    <button class="btn btn-danger btn-sm table-del" type="button"><i class="fa fa-trash"></i> 删除</button>';
        }else{
            if(strpos($tool,'search')!== false){
                $str.='<button title="普通查询" class="btn btn-default btn-sm table-search" type="button" ><i class="glyphicon glyphicon-search"></i></button>&nbsp;';
            }

            if(strpos($tool,'refresh')!== false){
                $str.='<a title="刷新" href="javascript:location.replace(location.href);" class="btn btn-default btn-add btn-sm">
                        <i class="fa fa-refresh"></i>
                    </a>&nbsp;';
            }

            if(strpos($tool,'add')!== false){
                $str.='<button class="btn btn-primary btn-sm table-add" type="button"><i class="fa fa-plus"></i> 添加</button>&nbsp;';
            }

            if(strpos($tool,'del')!== false){
                $str.='<button class="btn btn-danger btn-sm table-del" type="button" ><i class="fa fa-trash"></i> 删除</button>&nbsp;';
            }
        }
        echo $str;
    }

    /**
     * 查询按钮
     */
    public function search_btn(){
        $str='<div class="form-group" style="margin-left: 10px;">
                                    <button class="btn btn-primary btn-sm table-submit" type="button">提交</button>
                                    <button class="btn btn-white btn-sm table-reset" type="button">重置</button>
                                </div>
            ';
        echo $str;
    }

    /**
     * 表单提交按钮
     */
    public function submit_btn(){
        $str='<div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="button" id="btn_sub">确定</button>
                                <button class="btn btn-white" type="button" onclick="layer_close();">取消</button>
                            </div>
                        </div>';
        echo $str;
    }

    /**
     * tips组件
     * 问号提示
     */
    public function tips($msg){
        $str='<a onmouseover="layer_tips(\''.$msg.'\',this);"
                onmouseout="layer.closeAll(\'tips\');">
                <i class="fa fa-question-circle"></i></a>';
        echo $str;
    }

    /**
     * 时间控件
     * @param $name
     * @param $value
     * @return string
     */
    public function time_input($name,$value){
        $str='<div class="input-group date">
                                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                    <input type="text" name="'.$name.'" class="form-control" value="'.$value.'" readonly>
                                </div>';
        return $str;
    }
}